package com.yinhe.music.yhmusic.singer.info;

import android.graphics.drawable.Drawable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.InstrumentInfoAdapter;
import com.yinhe.music.yhmusic.adapter.SingerInfoAdapter;
import com.yinhe.music.yhmusic.base.BaseBarStateChangeListener;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.constants.KeyConstants;
import com.yinhe.music.yhmusic.instrument.InstrumentBean;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import butterknife.BindView;

/**
 * 歌手详细页
 *
 * @author hasee
 * @date 2017/7/13
 */

public class SingerInfoActivity extends BaseServiceActivity implements ISingerInfoContract.ISingerInfoView {
    @BindView(R.id.backdrop)
    ImageView mBackdrop;
    @BindView(R.id.collect_state)
    TextView collectState;
    @BindView(R.id.progressbar)
    ProgressBar progressBar;
    @BindView(R.id.collect_layout)
    LinearLayout collectLayout;
    @BindView(R.id.transparent_toolbar)
    Toolbar toolbar;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.header)
    FrameLayout header;
    @BindView(R.id.collapsingToolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.bottom_container)
    FrameLayout bottomContainer;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.transparent_toolbar_title)
    TextView toolbarTitle;
    private int singerid;
    private ActionBar actionBar;
    private int isFollow;
    private InstrumentBean.InstrumentListBean mInstrument;
    private SingerInfoPresenter mPresenter;

    String idParam = "singerId";

    @Override
    public int getLayoutResId() {
        return R.layout.activity_singer_detail;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SingerInfoPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    public void initView() {
        if (getIntent() != null) {
            singerid = getIntent().getIntExtra(idParam, 0);
            mInstrument = (InstrumentBean.InstrumentListBean) getIntent().getSerializableExtra(KeyConstants.KEY_INSTRUMENT);
        }

        hideToolbar();
        setupToolbar();
        showQuickControl(true);

        if (mInstrument == null) {
            mPresenter.getSingerInfo(singerid);
        } else {
            mPresenter.getInstrumentInfo(mInstrument.getInstrumentId());
        }

    }

    /**
     * 设置可折叠标题栏
     */
    private void setupToolbar() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("");
        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle("");
        }
        appbar.addOnOffsetChangedListener(new BaseBarStateChangeListener() {

            @Override
            public void onOffSetChanged(AppBarLayout appBarLayout, int i) {
                actionBar.setBackgroundDrawable(null);
                float a = (float) Math.abs(i) / (appBarLayout.getTotalScrollRange());
                collectLayout.setAlpha(1f - a);
            }

            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    //展开状态
                    if (mInstrument == null) {
                        collectLayout.setVisibility(View.VISIBLE);
                    } else {
                        toolbarTitle.setText(mInstrument.getInstrumentName());
                    }

                } else if (state == State.COLLAPSED) {
                    //折叠状态
                    collectLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 初始化ViewPager
     */
    private void setViewPager(SingerList mSingerInfo) {
        tabLayout.setupWithViewPager(viewpager);
        SingerInfoAdapter mPagerAdapter = new SingerInfoAdapter(getSupportFragmentManager(), mSingerInfo);
        viewpager.setAdapter(mPagerAdapter);
        ViewUtils.reflexIndicator(tabLayout, 15);
    }

    private void setInstrumentViewPager(InstrumentBean.InstrumentListBean bean) {
        tabLayout.setupWithViewPager(viewpager);
        InstrumentInfoAdapter mPagerAdapter = new InstrumentInfoAdapter(getSupportFragmentManager(), bean);
        viewpager.setAdapter(mPagerAdapter);
        ViewUtils.reflexIndicator(tabLayout, 15);
    }

    /**
     * 收藏点击
     */
    private void setFollow() {
        if (!Preferences.getLoginSn().isEmpty()) {
            mPresenter.collect(singerid, IConstants.SINGER, isFollow);
        } else {
            startLogin();
        }
    }

    @Override
    public void setSingerInfoUI(SingerList singerList) {
        toolbarTitle.setText(singerList.getSingerName());
        GlideHelper.setImageResource(mBackdrop, singerList.getImage(), R.drawable.default_cover);
        initCollectStatus(singerList.getCollectStatus());
        isFollow = singerList.getCollectStatus();
        collectLayout.setOnClickListener(v -> setFollow());
        setViewPager(singerList);
    }

    @Override
    public void seInstrumentInfoUI(InstrumentBean.InstrumentListBean instrumentListBean) {
        mInstrument = instrumentListBean;
        toolbarTitle.setText(mInstrument.getInstrumentName());
        GlideHelper.setImageResource(mBackdrop, mInstrument.getImage(), R.drawable.default_cover);
        collectLayout.setVisibility(View.GONE);
        setInstrumentViewPager(mInstrument);
    }

    private void initCollectStatus(int hasCollect) {
        if (hasCollect == 0) {
            collectState.setText("关注");
            Drawable drawable = getResources().getDrawable(R.drawable.ic_add_follow);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            collectState.setCompoundDrawables(drawable, null, null, null);
        } else if (hasCollect == 1) {
            collectState.setText("已关注");
            Drawable drawable = getResources().getDrawable(R.drawable.ic_has_sub);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            collectState.setCompoundDrawables(drawable, null, null, null);
        }
    }

    @Override
    public void updateCollectUI() {
        if (isFollow == 0) {
            UmengEventUtils.collect(this, UmengEventUtils.SINGER);
            isFollow = 1;
        } else {
            isFollow = 0;
        }
        initCollectStatus(isFollow);
        RxBus.get().post(RxBusEventType.Singer.COLLECT_SINGER, singerid);
    }

}
